import { isObject, isBoolean, omit } from 'lodash'

import qs from 'qs'

const CACHE = {
  enable: false,
  timeout: 5 * 1000,
  failedReq: true
}

export function getCacheConfig (cache) {
  if (isBoolean(cache) && cache) {
    return CACHE
  }

  if (isObject(cache)) {
    return Object.assign({}, CACHE, cache)
  }

  return CACHE
}

export const generateRequestKey = (config) => {
  const { url, headers, method, params, data } = config
  return qs.stringify({ url, data, headers, method, params: omit(params, '_t') })
}

export const generateCancelKey = (config) => {
  const { url, headers, method } = config
  return qs.stringify({ url, headers, method })
}

export const parseRequestKey = (key) => {
  return qs.parse(key)
}
